package com.abe.config;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Column;
import lecho.lib.hellocharts.model.ColumnChartData;
import lecho.lib.hellocharts.model.ComboLineColumnChartData;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.SubcolumnValue;

import com.abe.app.ManagerAPP;
import com.abe.mvp.model.bean.PatternBean;
import com.abe.threadmill.R;

/**
 * 跑步模式相关 速度為km/h
 * */
public class PatternConstants {

	/**************************** 模式 **********************************/
	public static Float[] model_1_speeds = { 6f, 8f, 9f, 12f, 12f, 9f, 8f, 5f };
	public static Float[] model_2_speeds = { 6f, 6f, 7f, 8f, 9f, 9f, 9f, 9f,
			7f, 7f, 6f };
	public static Float[] model_3_speeds = { 6.5f, 8f, 8f, 9f, 9f, 9f, 9f, 8f,
			6.5f, 6.5f, 6f };
	public static Float[] model_4_speeds = { 6f, 8f, 9f, 12f, 12f, 9f, 8f, 6f };
	public static Float[] model_5_speeds = { 6f, 6f, 8f, 8f, 12f, 9f, 12f, 12f,
			8f, 8f, 6f };
	public static Float[] model_6_speeds = { 6.5f, 8f, 8f, 9f, 12f, 12f, 9f, 8f,
			6.5f, 6.5f, 6f };

	public static Float[] model_1_slopes = { 6f, 6f, 8f, 9f, 9f, 9f, 6f, 6f };
	public static Float[] model_2_slopes = { 6f, 6f, 6f, 6f, 9f, 12f, 9f, 9f,
			6f, 6f, 6f };
	public static Float[] model_3_slopes = { 6f, 6f, 6f, 8f, 9f, 8f, 8f, 6f,
			6f, 6f, 6f };
	public static Float[] model_4_slopes = { 6f, 8f, 9f, 12f, 12f, 9f, 8f, 6f };
	public static Float[] model_5_slopes = { 6f, 6f, 8f, 8f, 12f, 5f, 12f, 12f,
			8f, 8f, 6f };
	public static Float[] model_6_slopes = { 6f, 6f, 8f, 8f, 9f, 9f, 9f, 6f,
			6f, 6f, 6f };

	/** 获取模式数据，速度单位为km/h */
	public static Float[][] model_speeds = { model_1_speeds, model_2_speeds,
			model_3_speeds, model_4_speeds, model_5_speeds, model_6_speeds };
	public static Float[][] model_slopes = { model_1_slopes, model_2_slopes,
			model_3_slopes, model_4_slopes, model_5_slopes, model_6_slopes };

	/** 获取模式数据，速度单位为m/s */
	public static Float[][] PatternSpeed() {
		Float[][] returnMsSpeed = new Float[model_speeds.length][];
		for (int i = 0; i < model_speeds.length; i++) {
			returnMsSpeed[i] = SpeedConvertByKMHArray(model_speeds[i]);
		}
		return returnMsSpeed;
	}

	/** 获取模式数据，速度单位为m/s */
	public static Float[] SpeedConvertByKMHArray(Float[] kmhSpeed) {
		Float[] returnMsSpeed = new Float[kmhSpeed.length];
		for (int i = 0; i < kmhSpeed.length; i++) {
			returnMsSpeed[i] = Constants.SpeedConvertByKMH(kmhSpeed[i]);
		}
		return returnMsSpeed;
	}

	/** 获取原始模式数据 */
	public static List<PatternBean> getPatternBeans() {
		List<PatternBean> patternBeans = new ArrayList<PatternBean>();
		for (int i = 0; i < 6; i++) {
			PatternBean bean = new PatternBean();
			ComboLineColumnChartData data = getComboLineColumnChartData(
					getListByArray(model_speeds[i]),
					getListByArray(model_slopes[i]));
			bean.setData(data);
			bean.setName("模式" + i);
			bean.setColor(ManagerAPP.getContext().getResources()
					.getColor(R.color.transparent));
			patternBeans.add(bean);
		}
		return patternBeans;
	}

	/**********************************************************/
	public static List<Float> getListByArray(Float[] array) {
		List<Float> list = new ArrayList<Float>();
		for (int i = 0; i < array.length; i++) {
			list.add(array[i]);
		}
		return list;
	}

	public static ComboLineColumnChartData getComboLineColumnChartData(
			List<Float> speeds, List<Float> slopes) {
		ComboLineColumnChartData data;
		/** 是否需要坐标轴 */
		boolean hasAxes = false;
		boolean hasAxesNames = false;
		data = new ComboLineColumnChartData(getColumnChartData(speeds),
				getLineChartData(slopes));
		/** 定义x轴y轴相应参数 */
		if (hasAxes) {
			Axis axisX = new Axis();
			/** 是否显示网格线 */
			Axis axisY = new Axis().setHasLines(true);
			if (hasAxesNames) {
				/** 轴名称 */
				axisX.setName("Axis X");
				axisY.setName("Axis Y");
			}
			/** 颜色 */
			axisX.setTextColor(ManagerAPP.getContext().getResources()
					.getColor(R.color.pattern_combolinechart_color));
			axisY.setTextColor(ManagerAPP.getContext().getResources()
					.getColor(R.color.pattern_combolinechart_color));
			/** 把X轴Y轴数据设置到ColumnChartData 对象中 */
			data.setAxisXBottom(axisX);
			data.setAxisYLeft(axisY);
		} else {
			data.setAxisXBottom(null);
			data.setAxisYLeft(null);
		}
		return data;
	}

	/**
	 * 获取折线图数据
	 * 
	 * @param heights
	 *            柱状高度数据
	 * */
	public static LineChartData getLineChartData(List<Float> heights) {
		/** 是否用线显示 */
		boolean hasLines = true;
		/** 是否平滑（曲折） */
		boolean isCubic = false;
		/** 是否只用点显示 */
		boolean hasPoints = false;
		/** 是否填充曲线的面积 */
		boolean isFilled = false;
		boolean hasLabels = false;
		boolean hasLabelForSelected = false;
		/** 使用的8列，每列1个subcolumn */
		int numberOfPoints = heights.size();
		int numberOfLines = 2;

		List<Line> lines = new ArrayList<Line>();
		for (int i = 0; i < numberOfLines; ++i) {
			if (i == 0) {
				List<PointValue> values = new ArrayList<PointValue>();
				for (int j = 0; j < numberOfPoints; ++j) {
					values.add(new PointValue(j, heights.get(j)));
				}
				Line line = new Line(values);
				line.setColor(ManagerAPP.getContext().getResources()
						.getColor(R.color.pattern_combolinechart_color));
				line.setCubic(isCubic);
				line.setFilled(isFilled);
				line.setHasPoints(hasPoints);
				line.setHasLines(hasLines);
				line.setHasLabels(hasLabels);
				line.setHasLabelsOnlyForSelected(hasLabelForSelected);
				lines.add(line);
			} else {
				List<PointValue> values = new ArrayList<PointValue>();
				for (int j = 0; j < numberOfPoints; ++j) {
					values.add(new PointValue(j, 20));
				}
				Line line = new Line(values);
				line.setColor(ManagerAPP.getContext().getResources()
						.getColor(R.color.transparent));
				line.setCubic(isCubic);
				line.setFilled(isFilled);
				line.setHasPoints(hasPoints);
				line.setHasLines(hasLines);
				line.setHasLabels(hasLabels);
				line.setHasLabelsOnlyForSelected(hasLabelForSelected);
				lines.add(line);
			}
		}
		LineChartData lineChartData = new LineChartData(lines);
		return lineChartData;
	}

	/**
	 * 获取柱状图数据
	 * 
	 * @param heights
	 *            柱状高度数据
	 * */
	public static ColumnChartData getColumnChartData(List<Float> heights) {
		ColumnChartData data;
		boolean hasLabels = false;
		boolean hasLabelForSelected = false;
		/** 使用的8列，每列1个subcolumn */
		int numSubcolumns = 1;
		int numColumns = heights.size();
		/** 定义一个圆柱对象集合 */
		List<Column> columns = new ArrayList<Column>();
		List<AxisValue> axisValues = new ArrayList<AxisValue>();
		/** 子列数据集合 */
		List<SubcolumnValue> values;
		/** 遍历列数numColumns */
		for (int i = 0; i < numColumns; ++i) {
			values = new ArrayList<SubcolumnValue>();
			/** 遍历每一列的每一个子列 */
			for (int j = 0; j < numSubcolumns; ++j) {
				/** 为每一柱图添加颜色和数值 */
				values.add(new SubcolumnValue(heights.get(i), ManagerAPP
						.getContext().getResources()
						.getColor(R.color.pattern_columnchart_color)));
			}
			/** 创建Column对象 */
			Column column = new Column(values);
			/** 是否有数据标注 */
			column.setHasLabels(hasLabels);
			/** 是否是点击圆柱才显示数据标注 */
			column.setHasLabelsOnlyForSelected(hasLabelForSelected);
			columns.add(column);
			/** 给x轴坐标设置描述 */
			axisValues.add(new AxisValue(i).setLabel(String.valueOf(i)));
		}
		/** 创建一个带有之前圆柱对象column集合的ColumnChartData */
		data = new ColumnChartData(columns);

		return data;
	}
}
